package com.fish.admin.module.tenant.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fish.admin.constant.TenantConstants;
import com.fish.admin.module.auth.domain.entity.AuthInfo;
import com.fish.admin.module.auth.manager.AuthInfoManager;
import com.fish.admin.module.system.domain.entity.SysRole;
import com.fish.admin.module.system.manager.SysRoleManager;
import com.fish.admin.module.tenant.domain.bo.TenantInfoBO;
import com.fish.admin.module.tenant.domain.dto.TenantInfoDTO;
import com.fish.admin.module.tenant.domain.dto.TenantInfoPageQueryDTO;
import com.fish.admin.module.tenant.domain.dto.TenantInfoQuotaDTO;
import com.fish.admin.module.tenant.domain.dto.TenantInfoRatioDTO;
import com.fish.admin.module.tenant.domain.entity.TenantInfo;
import com.fish.admin.module.tenant.domain.vo.TenantInfoVO;
import com.fish.admin.module.tenant.enums.*;
import com.fish.admin.module.tenant.manager.TenantInfoManager;
import com.fish.admin.module.tenant.service.TenantInfoService;
import com.fish.admin.util.UniqueIDGenerator;
import com.fish.common.core.domain.vo.PageVO;
import com.fish.common.core.enums.EnabledEnum;
import com.fish.common.core.enums.ReadonlyEnum;
import com.fish.common.core.enums.StatusEnum;
import com.fish.common.core.enums.UserTypeEnum;
import com.fish.common.mybatisplus.util.PageUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class TenantInfoServiceImpl implements TenantInfoService {

    private final TenantInfoManager tenantInfoManager;


    private final AuthInfoManager authInfoManager;

    private final SysRoleManager sysRoleManager;
    @Override
    public PageVO<TenantInfoVO> page(TenantInfoPageQueryDTO tenantInfoPageQueryDTO) {
        IPage<TenantInfoBO> page =tenantInfoManager.page(tenantInfoPageQueryDTO);
        return PageUtils.toPage(page, (e -> {
            TenantInfoVO v = new TenantInfoVO();
            BeanUtils.copyProperties(e, v);
            v.setInviteLink("https://localhost:8080/?code="+e.getTenantNo());
            return v;
        }));
    }

    @Override
    @Transactional(rollbackFor =Exception.class)
    public void save(TenantInfoDTO tenantInfoDTO) {
        //新增渠道基础信息
        TenantInfo tenantInfo = new TenantInfo();
        BeanUtils.copyProperties(tenantInfoDTO, tenantInfo);
        tenantInfo.setStatus(EnabledEnum.YES.getValue());
        tenantInfo.setEnabled(EnabledEnum.YES.getValue());
        tenantInfo.setTenantNo(UniqueIDGenerator.generateUniqueIDs());
        tenantInfo.setQuota(TenantConstants.DEFAULT_QUOTA);
        tenantInfo.setLoanQuota(TenantConstants.DEFAULT_LOAN_QUOTA);
        tenantInfo.setRechargeFeeRatio(TenantConstants.DEFAULT_RECHARGE_FEE_RATIO);
        tenantInfo.setWithdrawFeeRatio(TenantConstants.DEFAULT_WITHDRAW_FEE_RATIO);
        tenantInfo.setApiFeeRatio(TenantConstants.DEFAULT_API_FEE_RATIO);
        tenantInfo.setLoanAuth(LoanAuthEnums.NO.getValue());
        tenantInfo.setUpDownAuth(UpDownAuthEnums.NO.getValue());
        tenantInfo.setStealStatus(StealStatusEnums.NO.getValue());
        tenantInfo.setBoxActivity(BoxActivityEnums.NO.getValue());
        tenantInfo.setInviteReward(InviteRewardEnums.NO.getValue());
        tenantInfoManager.save(tenantInfo);


        //新增用户登录信息
        AuthInfo authInfo = new AuthInfo();
        authInfo.setAccount(tenantInfo.getAccount());
        authInfo.setSecret(tenantInfoDTO.getSecret());
        authInfo.setUserId(tenantInfo.getId());
        authInfo.setUserType(UserTypeEnum.TENANT.getValue());
        authInfo.setEnabled(EnabledEnum.YES.getValue());
        authInfo.setStatus(StatusEnum.YES.getValue());
        authInfo.setReadonly(ReadonlyEnum.YES.getValue());
        authInfo.setTenantId(tenantInfo.getId());
        authInfo.setTenantNo(tenantInfo.getTenantNo());
        authInfoManager.save(authInfo);

        //构建默认的角色
        SysRole manageRole = new SysRole();
        manageRole.setName("业务主管");
        manageRole.setSort(1);
        manageRole.setRemark("业务主管");
        buildTenantRole(tenantInfo, authInfo, manageRole);
        sysRoleManager.save(manageRole);

        SysRole businessRole = new SysRole();
        businessRole.setName("业务员");
        businessRole.setSort(2);
        businessRole.setRemark("业务员");
        buildTenantRole(tenantInfo, authInfo, businessRole);
        sysRoleManager.save(businessRole);
    }

    private void buildTenantRole(TenantInfo tenantInfo, AuthInfo authInfo, SysRole sysRole) {
        sysRole.setEnabled(EnabledEnum.YES.getValue());
        sysRole.setReadonly(ReadonlyEnum.YES.getValue());
        sysRole.setStatus(StatusEnum.YES.getValue());
        sysRole.setCreatorId(tenantInfo.getId());
        sysRole.setModifierId(tenantInfo.getId());
        sysRole.setCreator(tenantInfo.getAccount());
        sysRole.setModifier(tenantInfo.getAccount());
        sysRole.setTenantId(tenantInfo.getId());
        sysRole.setTenantNo(tenantInfo.getTenantNo());

    }

    @Override
    public void changeEnabled(Long id, EnabledEnum enabledEnum) {
        TenantInfo tenantInfo = new TenantInfo();
        tenantInfo.setId(id);
        tenantInfo.setEnabled(enabledEnum.getValue());
        tenantInfoManager.updateById(tenantInfo);
    }

    @Override
    public void changeLoanAuth(Long id, LoanAuthEnums loanAuthEnums) {
        TenantInfo tenantInfo = new TenantInfo();
        tenantInfo.setId(id);
        tenantInfo.setLoanAuth(loanAuthEnums.getValue());
        tenantInfoManager.updateById(tenantInfo);
    }

    @Override
    public void changeUpDownAuth(Long id, UpDownAuthEnums upDownAuthEnums) {
        TenantInfo tenantInfo = new TenantInfo();
        tenantInfo.setId(id);
        tenantInfo.setUpDownAuth(upDownAuthEnums.getValue());
        tenantInfoManager.updateById(tenantInfo);
    }

    @Override
    public void changeStealStatus(Long id, StealStatusEnums stealStatusEnums) {
        TenantInfo tenantInfo = new TenantInfo();
        tenantInfo.setId(id);
        tenantInfo.setStealStatus(stealStatusEnums.getValue());
        tenantInfoManager.updateById(tenantInfo);
    }

    @Override
    public void changeBoxActivity(Long id, BoxActivityEnums boxActivityEnums) {
        TenantInfo tenantInfo = new TenantInfo();
        tenantInfo.setId(id);
        tenantInfo.setBoxActivity(boxActivityEnums.getValue());
        tenantInfoManager.updateById(tenantInfo);
    }

    @Override
    public void changeInviteReward(Long id, InviteRewardEnums inviteRewardEnums) {
        TenantInfo tenantInfo = new TenantInfo();
        tenantInfo.setId(id);
        tenantInfo.setInviteReward(inviteRewardEnums.getValue());
        tenantInfoManager.updateById(tenantInfo);
    }

    @Override
    public void updateQuota(Long id,TenantInfoQuotaDTO tenantInfoQuotaDTO) {
        TenantInfo tenantInfo = new TenantInfo();
        tenantInfo.setId(id);
        tenantInfo.setQuota(tenantInfoQuotaDTO.getQuota());
        tenantInfo.setLoanQuota(tenantInfoQuotaDTO.getLoanQuota());
        tenantInfoManager.updateById(tenantInfo);

    }

    @Override
    public void updateRatio(Long id, TenantInfoRatioDTO techInfoRatioDTO) {
        TenantInfo tenantInfo = new TenantInfo();
        tenantInfo.setId(id);
        tenantInfo.setRechargeFeeRatio(techInfoRatioDTO.getRechargeFeeRatio());
        tenantInfo.setWithdrawFeeRatio(techInfoRatioDTO.getWithdrawFeeRatio());
        tenantInfo.setApiFeeRatio(techInfoRatioDTO.getApiFeeRatio());
        tenantInfoManager.updateById(tenantInfo);
    }



}
